﻿/*
    Copyright 2008 Travis Scholtens
    
    This file is part of Anticipa-Do List.

    Anticipa-Do List is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Anticipa-Do List is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Anticipa-Do List.  If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.IO;

using Anticipa_Do_List;

namespace TestNotifier
{
    public class TestNotificationManager : INotificationManager
    {
        private string file = "notifications.txt";

        private Dictionary<Guid, DateTime> notifications = new Dictionary<Guid, DateTime>();

        public string Description { get { return "Test Notification"; } }

        public List<KeyValuePair<string, bool>> GetConfigurationKeys()
        {
            var keys = new List<KeyValuePair<string, bool>>();
            keys.Add(new KeyValuePair<string, bool>("Filename", false));
            return keys;
        }

        public void SetConfigurationValues(List<string> values)
        {
            file = values[0];

            notifications.Clear();

            if (File.Exists(file))
                using (StreamReader r = new StreamReader(file))
                    for (string line = r.ReadLine(); line != null; line = r.ReadLine())
                    {
                        string[] parts = line.Split(new char[] { ' ' }, 2);
                        notifications.Add(new Guid(parts[0]), DateTime.Parse(parts[1]));
                    }
        }

        public void SetNotification(Guid id, string description, DateTime time)
        {
            if (notifications.ContainsKey(id))
                notifications[id] = time;
            else
                notifications.Add(id, time);

            using (StreamWriter w = new StreamWriter(file))
                foreach (var note in notifications)
                    w.WriteLine("{0} {1}", note.Key, note.Value);
        }
    }
}
